﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ProceduralGolf
{
    class TextureBlender : TextureEffect
    {
        private GraphicsDevice graphics;
        private TextureMasker masker;
       
        public TextureBlender(Game game)
        {
            this.graphics = game.GraphicsDevice;
            masker = new TextureMasker(game);
            spriteBatch = new SpriteBatch(graphics);
        }


        public Texture2D blend(Texture2D tex1, Texture2D tex2, Texture2D mask)
        {
            GraphicsMutex.GetMutex().WaitOne();

            Texture2D maskedTex1 = masker.mask(tex1, mask);
            Texture2D result;
            RenderTarget2D target = new RenderTarget2D(graphics, tex1.Width, tex1.Height, 1, graphics.PresentationParameters.BackBufferFormat);
            DepthStencilBuffer stencilBuffer = new DepthStencilBuffer(graphics, tex1.Width, tex1.Height, graphics.DepthStencilBuffer.Format);
            DepthStencilBuffer oldBuffer = graphics.DepthStencilBuffer;

            graphics.DepthStencilBuffer = stencilBuffer;
            graphics.SetRenderTarget(0, target);
            graphics.Clear(Color.Black);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            spriteBatch.Draw(tex2, new Rectangle(0, 0, tex2.Width, tex2.Height), Color.White);
            spriteBatch.Draw(maskedTex1, new Rectangle(0, 0, maskedTex1.Width, maskedTex1.Height), Color.White);
            spriteBatch.End();

            graphics.SetRenderTarget(0, null);
            graphics.DepthStencilBuffer = oldBuffer;

            result = target.GetTexture();
            stencilBuffer.Dispose();
            target.Dispose();

            GraphicsMutex.GetMutex().ReleaseMutex();

            return result;

        }

        //public Texture2D blend(Texture2D tex1, Texture2D tex2, Texture2D mask)
        //{
        //    Texture2D maskedTex1 = masker.mask(tex1, mask);
        //    Texture2D result;
        //    RenderTarget2D target = new RenderTarget2D(graphics, tex1.Width, tex1.Height, 1, graphics.PresentationParameters.BackBufferFormat);

        //    graphics.SetRenderTarget(0, target);

        //    spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
        //    spriteBatch.Draw(tex2, new Rectangle(0, 0, tex2.Width, tex2.Height), Color.White);
        //    spriteBatch.Draw(maskedTex1, new Rectangle(0, 0, maskedTex1.Width, maskedTex1.Height), Color.White);
        //    spriteBatch.End();

        //    graphics.SetRenderTarget(0, null);
        //    result = target.GetTexture();
        //    target.Dispose();

        //    return result;

        //}

    }
}
